package cn.uppower.wordaliy.activity;

import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import cn.iwgang.countdownview.CountdownView;
import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.WordaliyApplication;
import cn.uppower.wordaliy.fragment.TestFeagment;
import cn.uppower.wordaliy.model.PageWord;
import cn.uppower.wordaliy.util.CalendarUtil;
import cn.uppower.wordaliy.util.WordsUtils;
import cn.uppower.wordaliy.widget.BaseActivity;

public class TestActivity extends BaseActivity {

    CountdownView mCvCountdownView;
    ViewPager vp_test;

    private Map<Integer,Integer> trueWordList;

    private List<Fragment> fragments;
    private TextView tv_commit,tv_timu_number;

    private List<PageWord> pageWords;


    public static Map<Integer,Integer> userSelect =new LinkedHashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public Integer getLayoutResId() {
        return R.layout.activity_test;
    }

    @Override
    public void init() {

        mCvCountdownView = findViewById(R.id.cv_test);
        tv_commit = findViewById(R.id.tv_commit);
        tv_timu_number = findViewById(R.id.tv_timu_number);
        vp_test = findViewById(R.id.vp_test);
        mCvCountdownView.start(300000); // 毫秒
        mCvCountdownView.setOnCountdownEndListener(new CountdownView.OnCountdownEndListener() {
            @Override
            public void onEnd(CountdownView cv) {
                caculateScore();
            }
        });



        int wordsSize = WordaliyApplication.wordaliySharedPreferences.getInt("TestNumberOfWords",24);
        pageWords =WordsUtils.getYwxyWords(wordsSize);
        tv_timu_number.setText("题目:1"+"/"+pageWords.size());
        trueWordList=new LinkedHashMap<>();
        fragments=new ArrayList<>();
        Random random=new Random();
        for (PageWord pageWord:pageWords){
            int trueIndex= random.nextInt(4)+1;
            trueWordList.put(pageWord.getCorrectWords().getId(),trueIndex);
            fragments.add(new TestFeagment(pageWord,trueIndex));
//            初始化用户选择的结果集
            userSelect.put(pageWord.getCorrectWords().getId(),-1);
        }


        vp_test.setAdapter(new MyViewPagerAdapter(this.getSupportFragmentManager()));
        vp_test.setOffscreenPageLimit(wordsSize);
        vp_test.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv_timu_number.setText("题目:"+(position+1)+"/"+pageWords.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        tv_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (Integer i:userSelect.keySet()){
                    if (userSelect.get(i)==-1){
                        final AlertDialog.Builder normalDialog =
                                new AlertDialog.Builder(TestActivity.this);
                        normalDialog.setIcon(R.mipmap.wring);
                        normalDialog.setTitle("提交");
                        normalDialog.setMessage("您还有试题没有选择答案,是否继续提交?");
                        normalDialog.setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        caculateScore();
                                    }
                                });
                        normalDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        // 显示
                        normalDialog.show();
                        return;
                    }

                }
                caculateScore();
            }
        });
    }

    private void caculateScore() {
        //                当倒计时结束，直接提交试题，然后进行分数计算
        int trueWordsNumber =0 ;
        for (int i:trueWordList.keySet()){
            if (userSelect.get(i)==trueWordList.get(i)){
                trueWordsNumber++;
            }
        }
        double score = Double.valueOf(trueWordsNumber)/Double.valueOf(pageWords.size())*100;
        score =(double)Math.round(score*10)/10;

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(TestActivity.this);
        normalDialog.setIcon(R.mipmap.word_ok);
        normalDialog.setTitle("分数");
        normalDialog.setMessage("你的试题得分是:"+score);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        TestActivity.this.finish();
                    }
                });
        // 显示
        normalDialog.show();
    }


    class MyViewPagerAdapter extends FragmentPagerAdapter {


        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
